#include<bits/stdc++.h>
#define ll long long
#define pir pair<int,int>
using namespace std;
const int maxn=100005;
int n,m,a[maxn],t,T,d[25][maxn],f[maxn],ask[maxn];

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) cin>>ask[i];
    t=log2(n)+1;
    for(int i=1;i<=t;i++){
        string s;cin>>s;
        for(int j=0;j<1<<t-i;j++) d[i][j+1]=s[j]-'0';
    }
    for(int i=1;i<=t;i++){
        int k=0;
        for(int j=1;j<=1<<t-i;j++)
            for(int w=0;w<=1;w++){
                for(int cnt=1;cnt<=1<<i-1;cnt++)
                    f[k+cnt]=w==d[i][j]?i:f[k+cnt];
                k+=1<<i-1;
            }
    }
    cin>>T;
    while(T--){
        int x[4];
        for(int i=0;i<4;i++) cin>>x[i];
        vector<int>c(n+1);
        int ans=0;
        for(int i=1;i<=n;i++) c[i]=c[i-1]+(((a[i]^x[i%4])>=f[i])?i:0);
        for(int i=1,w;i<=m;i++){
            if((1<<(int)(log2(ask[i])))==ask[i]) ans=ans^(i*c[ask[i]]);
            else w=log2(ask[i])+1,w=1<<w+1,w=(ask[i]+1+w)*(w-ask[i])/2,ans=ans^((c[ask[i]]+w)*i);
        }
        cout<<ans<<"\n";
    }
    return 0;
}